<?php
namespace Admin\Controller;
use Org\Util\String;
use Think\Controller;
use Admin\Api\CarTermApi;
use Weixin\Api\WxaccountApi;

class WxaccountController extends BaseController {
    /*
     * 账号资料编辑
     * */
    public function edit(){

        if(IS_GET){
            $map = array('id'=>'6');
            $result = apiCall(WxaccountApi::GET_INFO,array($map));

            if($result['status']){
                $this->assign("wxaccount",$result['info']);
                $this->display();
            }else{
                $this->error($result['info']);
            }
        }else{
            import("Org.Util.String");
            $id= I('post.id',0,'intval');
            $len = 43;

            $EncodingAESKey= I('post.encodingAESKey','');
            $tokenvalue = I('post.token','');
            if(empty($EncodingAESKey)){
                $EncodingAESKey =  String::randString($len,0,'0123456789');
            }

            if(empty($tokenvalue)){
                $tokenvalue = String::randString(8,3);
            }

            $entity = array(
                'wxname'=>I('post.wxname',''),
                'appid'=>I('post.appid'),
                'appsecret'=>I('post.appsecret'),
//				'token'=>I('post.token'),
                'weixin'=>I('post.weixin'),
                'headerpic'=>I('post.headerpic',''),
                'qrcode'=>I('post.qrcode',''),
                'wxuid'=>I('post.wxuid'),
                'encodingAESKey'=>$EncodingAESKey,
            );

            if(!empty($id) && $id > 0){

                $result = apiCall(WxaccountApi::SAVE_BY_ID, array($id, $entity));
                if ($result['status'] === false) {

                    $this -> error($result['info']);
                } else {
                    $this -> success(L('RESULT_SUCCESS'), U('Admin/Wxaccount/edit'));
                }
            }else{
                $entity['uid'] = 1;
                $entity['token'] = $tokenvalue.time();
                $result = apiCall(WxaccountApi::ADD, array($entity));

                if ($result['status'] === false) {

                    $this -> error($result['info']);
                } else {
                    $this -> success(L('RESULT_SUCCESS'),  U('Admin/Wxaccount/edit'));
                }
            }
        }
    }


    /**
     * 公众号帮助信息
     */
    public function help(){

        if(IS_GET){
            $map = array('id'=>'6');
            $result = apiCall(WxaccountApi::GET_INFO,array($map));

            if($result['status']){
                $this->assign("wxaccount",$result['info']);
                $this->display();
            }else{
                $this->error($result['info']);
            }
        }
    }


}